Repository navigation
Phase 1: wallet state digest (ECMH over ristretto255), logged every 1000 blocks - #14
Merged
Merged
Conversation
…lisation Phase 1 of the state-root plan agreed with BK: an order-independent digest of the wallet state that every node can compute and compare, out of block, so nothing changes in the protocol. - cs::MultisetHash: elliptic-curve multiset hash over ristretto255 (libsodium, already linked through cscrypto). An item is mapped to a group element via BLAKE2b-512 and crypto_core_ristretto255_from_hash. The digest is the group sum, starting from the identity (32 zero bytes). Adding or removing an item costs O(1), about 50 us, independent of state size, and the result does not depend on insertion order. ristretto255 is used instead of raw ed25519 to avoid cofactor issues. - cs::serializeWalletState(): canonical, versioned serialisation of the balance-relevant state of a wallet: key, balance, delegated amount, and delegations in both directions. Counterparts are sorted by key (std::map); entries are sorted because their order depends on apply/revert history. Empty for a wallet without such state, so empty records never move the digest. Little-endian, fixed-width fields. Tests (tests/csnode/statedigest_tests.cpp): order independence, add/remove inverse, distinct sets, empty wallet, balance and key sensitivity, delegation order independence. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… blocks Wire the ECMH digest into MultiWallets, the single place where wallet records are inserted or replaced (onWalletCacheUpdated). - The digest is computed in full on the first call to stateDigest(). Node calls it right after the blockchain is loaded (slow or quick start), before taking part in the network. From then on, each wallet update removes the wallet's previous element and adds the new one. Slow-start replay does not pay a per-update cost. - The element of each wallet is stored in its record (stateElement_). The delegation maps are shared_ptrs that Staking updates in place before the record is replaced, so re-serialising the old record would remove the wrong element. The element is not persisted; it is rebuilt at start. - WalletsCache_Serializer aliases the MultiWallets container through a mirror WalletData type, so the mirror gets the same two fields (not serialised: the checkpoint format is unchanged). A static_assert on the two sizes now catches any future mismatch at compile time. - Node logs "STATE DIGEST #<seq> <hex> wallets <n>" at start and on every block whose sequence is a multiple of 1000. Healthy nodes print the same digest for the same sequence; a different one pinpoints a divergent node and, between two log points, the block range where it diverged. No protocol, consensus or storage format change. nodetests: 132/132 pass; the node binary builds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 1 of the state-root plan (agreed with BK)
An order-independent digest of the wallet state that every node computes and logs. It travels outside the block: no change to the protocol, the consensus rules or the storage format, and the node version is unchanged. Its purpose is to see balance divergence, like the one that left nodes stuck at block 176'776'210, instead of finding out when a node stops.
Commit 1: the primitive
cs::MultisetHash: ECMH over ristretto255 (libsodium, already linked through cscrypto). Each item is BLAKE2b-512 mapped to the group withcrypto_core_ristretto255_from_hash, and the digest is the group sum. Adding or removing an item costs O(1), about 50 us regardless of state size, and the result does not depend on order.cs::serializeWalletState(): canonical, versioned serialisation of key, balance, delegated amount and delegations in both directions. Delegations are sorted, because their order depends on apply/revert history. A wallet without state serialises to nothing and does not move the digest.Commit 2: the integration
MultiWallets::onWalletCacheUpdated(), the single place where records are inserted or replaced, each update removes the wallet's previous element and adds the new one. The full computation happens once, at start after the chain is loaded, so slow start does not pay a per-update cost.shared_ptrs thatStakingupdates in place, so re-serialising the old record would remove the wrong element.WalletsCache_Serializeraliases the container through a mirrorWalletData. The mirror gets the same fields, not serialised, so the checkpoint format is unchanged, and astatic_asserton the sizes turns any future mismatch into a compile error.STATE DIGEST #<seq> <hex> wallets <n>at start and every 1'000 blocks.How to use it
Healthy nodes at the same sequence print the same digest. A different digest points to a divergent node and, between two log lines, to the block range where the divergence started. It is also the data needed to choose the majority state before Phase 2.
Verification and limits
nodetests132/132 (6 new tests); the node binary builds.Possible next steps
Expose the digest through the API and show it per node in BK's dashboard; add a digest of the contract state.
🤖 Generated with Claude Code